package com.wyw.leetcode.learning.simple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * leetcode topic 409
 * 最长回文串
 *
 * @Author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
public class Topic409 {

    static Map hashmap = new HashMap<Character, Integer>();
    public static void main(String[] args) {
        System.out.println(longestPalindrome("abccccdd"));
    }

    public static int longestPalindrome(String s) {
        char[] chars = s.toCharArray();
        int length = chars.length;
        for(int i = 0; i < length; i++) {
           if(hashmap.containsKey(chars[i])) {
                int result = (int) hashmap.get(chars[i]);
                hashmap.put(chars[i], ++result);
           } else {
               hashmap.put(chars[i], 1);
           }
        }

        int oddNum = 0;
//        Collection<Integer> values = hashmap.values();
//        final Collection values = hashmap.values();// 暂不知力扣编译为什么报错
        ArrayList<Integer> values = new ArrayList<Integer>(hashmap.values());
        for (Object value: values) {
            if((int)value%2 == 1) {
                oddNum++;
            }
        }
        if(oddNum > 1) {
            return length - oddNum + 1;
        }
        return length;
    }

    public int longestPalindrome1(String s) {
        int[] count = new int[128];
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            count[c]++;
        }

        int ans = 0;
        for (int v: count) {
            ans += v / 2 * 2;
            if (v % 2 == 1 && ans % 2 == 0) {// 首次奇数加一
                ans++;
            }
        }
        return ans;
    }
}
